package com.ddc.study.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ddc.study.user.UserManager;
import com.ddc.study.util.Log;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;

/**
 * Created on 2018/5/9.
 *
 * @author lshun
 * @version 1.0
 */
public abstract class BaseFragment extends Fragment {
    protected final String TAG = getClass().getSimpleName();

    @LayoutRes
    protected abstract int getLayoutRes();

    @Override
    public void onAttach(@NonNull Context context) {
        Log.e(TAG, "onAttach ^_^ ");
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Log.e(TAG, "onCreate ^_^ ");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        Log.e(TAG, "onCreateView ^_^ ");
        View view = inflater.inflate(getLayoutRes(), container, false);
        ButterKnife.bind(this, view);

        initView();

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        Log.e(TAG, "onActivityCreated ^_^ ");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        Log.e(TAG, "onStart ^_^ ");
        super.onStart();
    }

    @Override
    public void onResume() {
        Log.e(TAG, "onResume ^_^ ");
        super.onResume();
    }

    @Override
    public void onPause() {
        Log.e(TAG, "onPause ^_^ ");
        super.onPause();
    }

    @Override
    public void onStop() {
        Log.e(TAG, "onStop ^_^ ");
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        Log.e(TAG, "onDestroyView ^_^ ");
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy ^_^ ");
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        Log.e(TAG, "onDetach ^_^ ");
        super.onDetach();
    }

    /**
     * do the init things on this method if needed.
     */
    protected void initView() {
        Log.e(TAG, "initView ^_^ ");
    }

    public void showDialog() {
        if (getActivity() != null && getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).showDialog();
        }
    }

    public void showDialog(String msg) {
        if (getActivity() != null && getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).showDialog(msg);
        }
    }

    public void dismissDialog() {
        if (getActivity() != null && getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).dismissDialog();
        }
    }

    protected void showSoftInput() {
        if (getActivity() != null && getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).showSoftInput();
        }
    }

    protected void hideSoftInput() {
        if (getActivity() != null && getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).hideSoftInput();
        }
    }

    protected void showToast(@NonNull String text) {
        if (getActivity() != null && getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).showToast(text);
        }
    }

    protected void showToast(@StringRes int resId) {
        if (getActivity() != null && getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).showToast(resId);
        }
    }

    public boolean isLoggedIn() {
        return BaseApplication.getInstance().isLoggedIn();
    }

    protected void addLogInObserver(@NonNull UserManager.UserObserver observer) {
        BaseApplication.getInstance().observe(observer);
    }

    protected void removeLogInObserver(@NonNull UserManager.UserObserver observer) {
        BaseApplication.getInstance().removeObserver(observer);
    }
}
